class Res {
    let lock = false;
}

const res = new Res();

// 用于储存局部变量
class Context {
    let key = true;
}

function swap(res, ctx) {
    const temp = res.lock;
    res.lock = ctx.key;
    ctx.key = temp;
}

function process() {
    const ctx = new Context();
    while (ctx.key) {
        swap(res, ctx);
    }
    // use resource
    res.lock = false;
}
